﻿using MediatR;
using SlaughterItem.Common.Domains.SetUpScale;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Common.Infrastructures.Repositories;
using SlaughterItem.Enum;

namespace GetSlaughterItem.API.Application.Commands.Week02.Scale
{
    public class ScaleShowHandler : IRequestHandler<ScaleShowCommand, Result>
    {
        IBaseRepositoryGet<scaleContexts> ScaleContextRep;

        public ScaleShowHandler(IBaseRepositoryGet<scaleContexts> scaleContextRep)
        {
            ScaleContextRep = scaleContextRep;
        }

        /// <summary>
        /// 联查    根据仓库查询磅秤
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(ScaleShowCommand request, CancellationToken cancellationToken)
        {
                 var result = new Result() { Code = (int)CommonErrorCode.Search };
            try
            {
                var list = from a in ScaleContextRep.GetAll()
                           where a.BindDepotId == request.DepotId
                           select new
                           {
                               a.ScaleId,
                               a.ScaleName,
                               a.TareNum
                           };
                if (list != null)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Data = list;
                    result.Msg = "成功";
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
